package api

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	fromData "user_api/api_structure"
	"user_api/global"
	proto "user_api/proto"
	myvalidator "user_api/validator"
)

// AddUser 创建用户
func AddUser(ctx *gin.Context) {
	rep := &proto.RequestUserInfo{}
	AddUserData := fromData.AddUserForm{}

	if err := ctx.ShouldBind(&AddUserData); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	//赋值
	if err := global.SwapTo(AddUserData, rep); err != nil {
		return
	}
	zap.S().Debug("传输参数", rep)
	res, err := global.UserSrvClient.AddUser(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}

func GetUserInfo(ctx *gin.Context) {

	GetUserIdForm := fromData.GetUserIdForm{}
	if err := ctx.ShouldBind(&GetUserIdForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	rep := &proto.RequestUserId{Userid: GetUserIdForm.UserId}

	zap.S().Debug("获取用户信息", rep)

	res, err := global.UserSrvClient.GetUserInfoByUserid(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
}
func GetUserInfoByAcount(ctx *gin.Context) {

	GetUserAccountForm := fromData.GetUserAccountForm{}
	if err := ctx.ShouldBind(&GetUserAccountForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	rep := &proto.RequestAccount{Account: GetUserAccountForm.Account}

	zap.S().Debug("根据账户获取用户信息", rep)

	res, err := global.UserSrvClient.GetUserInfoByAccount(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}

}

func GetUserInfoByName(ctx *gin.Context) {

	GetUserNameForm := fromData.GetUserNameForm{}
	if err := ctx.ShouldBind(&GetUserNameForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	rep := &proto.RequestName{UserName: GetUserNameForm.UserName}

	zap.S().Debug("根据用户姓名获取用户信息", rep)

	res, err := global.UserSrvClient.GetUserInfoByName(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}

}

func GetUserInfoByMobile(ctx *gin.Context) {

	GetUserMobileForm := fromData.GetUserMobileForm{}
	if err := ctx.ShouldBind(&GetUserMobileForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	rep := &proto.RequestMobile{Mobile: GetUserMobileForm.Mobile}

	zap.S().Debug("根据手机号获取用户信息", rep)

	res, err := global.UserSrvClient.GetUserInfoByMobile(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}

}

func DelUser(ctx *gin.Context) {
	GetUserIdForm := fromData.GetUserIdForm{}
	if err := ctx.ShouldBind(&GetUserIdForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}

	rep := &proto.RequestUserId{Userid: GetUserIdForm.UserId}

	zap.S().Debug("根据手机号获取用户信息", rep)

	res, err := global.UserSrvClient.DelUser(context.TODO(), rep)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}

}

func GetUserList(ctx *gin.Context) {
	rep := &proto.RequestLimitList{}
	RequestLimitFrom := fromData.RequestLimitFrom{}

	if err := ctx.ShouldBind(&RequestLimitFrom); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}
	if err := global.SwapTo(RequestLimitFrom, rep); err != nil {
		return
	}
	zap.S().Debug("传输的参数", rep)

	res, err := global.UserSrvClient.GetUserList(context.TODO(), rep)
	zap.S().Debug("结果信息", res)

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}

func EditUser(ctx *gin.Context) {
	EditUserForm := fromData.EditUserForm{}

	if err := ctx.ShouldBind(&EditUserForm); err != nil {
		myvalidator.HandleValidatorError(ctx, err)
		return
	}
	res, err := global.UserSrvClient.EditUser(context.TODO(), &proto.RequestUserInfo{
		Id:          EditUserForm.Id,
		UserName:    EditUserForm.UserName,
		PassWord:    EditUserForm.PassWord,
		Departments: EditUserForm.Departments,
		Email:       EditUserForm.Email,
		Status:      EditUserForm.Status,
		Role:        EditUserForm.Role,
		Addr:        EditUserForm.Addr,
		Mobile:      EditUserForm.Mobile,
	})

	if err != nil {
		global.ReturnMsg(ctx, http.StatusForbidden, "no", err)
	} else {
		global.ReturnMsg(ctx, http.StatusOK, "ok", res)
	}
	return
}
